﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ProgNetComponentsDx.Options;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.Utils.Menu;

namespace BuildHelper
{
    public partial class MyBaseSqlCeGridForm : ProgNetComponentsDx.Forms.BaseSqlCeGridForm, IDataForm
    {
        public MyBaseSqlCeGridForm()
        {
            InitializeComponent();
           
            this.layoutProvider1.LayoutName = null;
            if (!this.IsDesignMode())
            {
                OptionCache.OptionChanged += new OptionChangedEventHandler(OptionChanged);
                GlobalTools.GridTools.AddGridRowIndicatorEvent(gcMain);
            }
        }

        ~MyBaseSqlCeGridForm()
        {
            OptionCache.OptionChanged -= new OptionChangedEventHandler(OptionChanged);
        }

        protected virtual void OptionChanged(object optionId)
        {
            if (OptionCache.IsTheSameId(optionId, Init.InitFormOptionNames.Rozne.AutoSaveLayoutAll))
            {
                layoutProvider1.AutoSaveLayoutsOnFormClosing = OptionCache.GetOptionBoolValue(optionId);
            }
        }
        
        protected override void OnLoad()
        {
            if (!this.IsDesignMode())
            {
                SetLayoutMenuItemPos();
                bar3.Visible = StatusBarVisible;
                bar2.Visible = MainMenuBarVisible;

                layoutProvider1.SaveCurrentLayoutAsDefault();
            }
            modeProvider1.CurrentMode = ProgNetComponentsDx.Enums.FormMode.None;
            base.OnLoad();
            bar3.Visible = StatusBarVisible;
            bar2.Visible = MainMenuBarVisible;
        }
        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.MdiFormClosing)
                e.Cancel = true;
            else
            {
                if (!Program.IsExit)
                {
                    FinishEdit();
                    if (changeDetectorProvider1.HasChanges)
                    {
                        DialogResult dlg = this.ShowDxYNCQuestionDialog("Chcesz zapisać zmiany?");
                        switch (dlg)
                        {
                            case DialogResult.Cancel:
                                e.Cancel = true;
                                break;
                            case DialogResult.No:
                                break;
                            case DialogResult.Yes:
                                if (ValidateAll())
                                {
                                    if (!Save())
                                    {
                                        this.ShowDxErrorDialog("Błąd zapisu!");
                                        e.Cancel = true;
                                    }
                                }
                                else
                                {
                                    this.ShowDxWarningDialog("Błąd validacji.");
                                    e.Cancel = true;
                                }
                                break;
                        }
                    }
                }
            }
            base.OnFormClosing(e);
        }
        protected virtual void SetLayoutMenuItemPos()
        {
            bar1.SetItemPosition(bsiLayout, bar1.LinksPersistInfo.Count);
        }
        protected virtual bool StatusBarVisible
        {
            get { return false; }
        }
        protected virtual bool MainMenuBarVisible
        {
            get { return false; }
        }
        #region IDataForm Members

        string IDataForm.Text
        {
            get { return Text; }
        }

        bool IDataForm.HasChanges
        {
            get { return changeDetectorProvider1.HasChanges; }
        }

        bool IDataForm.Save()
        {
            return Save();
        }

        bool IDataForm.ValidateAll()
        {
            return ValidateAll();
        }

        void IDataForm.FinishEdit()
        {
            FinishEdit();
        }

        #endregion

    }

    
}
